package com.example.recyclerview6;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.MyHolder> {

    private List<Product> products;

    ProductListAdapter(List<Product> products) {
        this.products = products;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 获取item布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        // 绑定数据
        Product product = products.get(position);
        // 设置
//        holder.cover.setImageDrawable();
        holder.name.setText(product.getName());
        holder.style.setText("风格：" + product.getStyle());
        holder.size.setText("尺寸：" + product.getSize());
        holder.price.setText("价格：￥" + product.getPrice().toString());
    }

    @Override
    public int getItemCount() {
        return products.size();
    }


    class MyHolder extends RecyclerView.ViewHolder {

        private ImageView cover;

        private TextView name;

        private TextView style;

        private TextView size;

        private TextView price;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            // 初始化控件
            cover = itemView.findViewById(R.id.imageView_cover);
            name = itemView.findViewById(R.id.textView_name);
            style = itemView.findViewById(R.id.textView_style);
            size = itemView.findViewById(R.id.textView_size);
            price = itemView.findViewById(R.id.textView_price);

        }
    }
}
